package com.apes.framework.util;

import java.util.HashMap;
import java.util.Map;

public class CreateEntity {
    public  static  final String  MASTER ="master";
    public  static  final String  ITEM ="item";
    public  static  final String  SUBSIDIARY ="sub";
    private static String createImport(String type,Map<String,String> map) {
        StringBuffer entityImp = new StringBuffer();
        entityImp.append("package ${packageName};\n");
        entityImp.append("import com.apes.framework.jpa.DefaultPersistListener;\n");
        entityImp.append("import org.hibernate.annotations.SelectBeforeUpdate;\n");
        entityImp.append("import org.hibernate.annotations.DynamicInsert;\n");
        entityImp.append("import org.hibernate.annotations.DynamicUpdate;\n");
        entityImp.append("import javax.persistence.*;\n");
        entityImp.append("import javax.validation.constraints.NotNull;\n");
        entityImp.append("import static java.lang.Math.*;\n");
        entityImp.append("import java.util.*;\n");
        entityImp.append("import com.apes.framework.jpa.tuple.date.DateTime;");
        entityImp.append("import com.apes.framework.jpa.comment.Comment;\n");
        entityImp.append("import com.apes.framework.jpa.generator.ApesGenerator;\n");
        entityImp.append("import com.apes.framework.jpa.${model};\n");
        if (type.equals(MASTER)) {
            entityImp.append("import com.apes.framework.plugin.soe.annotations.*;\n");
        }
        entityImp.append("${importEntity}\n\n");
        String resultStr = StringUtil.resolvePlaceholders(entityImp.toString(), map);
        return resultStr;
     }

    private static String createBody(String type,Map<String,String> map,boolean isMultiple) {
        StringBuffer entityBody = new StringBuffer();
        entityBody.append("@SelectBeforeUpdate\n");
        entityBody.append("@DynamicInsert\n");
        entityBody.append("@DynamicUpdate\n");
        entityBody.append("@Entity\n");
        entityBody.append("@EntityListeners(DefaultPersistListener.class)\n");
        entityBody.append("@Table(name = ${className}.CONST_TABLE_NAME${indexs})\n");
        entityBody.append("public class ${className} extends ${model} {\n");
        entityBody.append("    public static final String CONST_TABLE_NAME = \"${tableName}\";\n\n");
        entityBody.append("    @Id\n");
        entityBody.append("    @Column(name = \"id\", length = 32)\n");
        entityBody.append("    @ApesGenerator(code = CONST_TABLE_NAME, name = \"条件记录\", prefix = \"P\")\n");
        entityBody.append("    @Comment(\"单号\")\n");
        entityBody.append("    private String id;\n\n");
        if (type.equals(MASTER) && isMultiple){
            entityBody.append("    @States(name = \"订单状态\", values = {\n");
            entityBody.append("        @State(id = \"sent\", name = \"制单\"),\n");
            entityBody.append("        @State(id = \"approve\", name = \"审核\"),\n");
            entityBody.append("        @State(id = \"stop\", name = \"终止\")})\n");
            entityBody.append("    @Column(name = \"state\", length = 10, nullable = false)\n");
            entityBody.append("    private String state;\n");
            entityBody.append("    @OneToMany(mappedBy = \"master\", cascade = CascadeType.ALL, orphanRemoval = true)\n");
            entityBody.append("    private Set<${itemClassName}> items = new HashSet();\n\n");
        }else if (type.equals(ITEM)){
            entityBody.append("    @NotNull\n");
            entityBody.append("    @ManyToOne(optional = false)\n");
            entityBody.append("    @JoinColumn(name = \"master_id\", referencedColumnName = \"id\")\n");
            entityBody.append("    private ${masterClass} master;\n\n");
        }
        entityBody.append("${field}\n");
        entityBody.append("    public String getId() {\n" +
                          "      return id;\n" +
                          "    }\n");
        entityBody.append("    public void setId(String id) {\n" +
                          "       this.id = id;\n" +
                          "    }\n");
        if (type.equals(MASTER) && isMultiple){
            entityBody.append("    public Set<${itemClassName}> getItems() {\n" +
                              "      for(${itemClassName} item : items){\n" +
                              "        item.setMaster(this);\n"+
                              "      }\n"+
                              "      return items;\n" +
                              "    }\n");
            entityBody.append("    public void setItems(Set<${itemClassName}> items) {\n" +
                              "      this.items = items;\n" +
                              "    }\n");
            entityBody.append("    public String getState() {\n");
            entityBody.append("      return state;\n");
            entityBody.append("    }\n");
            entityBody.append("    public void setState(String state) {\n");
            entityBody.append("      this.state = state;\n");
            entityBody.append("    }\n");
        }else if (type.equals(ITEM)){
            entityBody.append("    public ${masterClass} getMaster() {\n" +
                              "      return master;\n" +
                              "    }\n");
            entityBody.append("    public void setMaster(${masterClass} master) {\n" +
                              "       this.master = master;\n" +
                              "    }\n");
        }
        entityBody.append("${method}\n");
        entityBody.append("}");
        String resultStr = StringUtil.resolvePlaceholders(entityBody.toString(), map);
        return resultStr;
    }

    public static String createModel(String packageName, String className,String tableName,String importEntity,String field,String method,String indexs,String type,boolean isMultiple){
        Map<String, String> map = new HashMap<String, String>();
        map.put("importEntity",importEntity);
        map.put("packageName",packageName+".model");
        map.put("masterField",StringUtil.toLowerCaseFirst(className));
        map.put("indexs","");
        if (type.equals(MASTER)){
            map.put("className",className);
            map.put("tableName",tableName);
            map.put("itemClassName",StringUtil.toUpperFirst(className)+"Items");
            map.put("itemClassNameField",StringUtil.toLowerCaseFirst(className)+"Items");
            map.put("model","Model");
        }else if (type.equals(ITEM)){
            map.put("className",className+"Items");
            map.put("tableName",tableName+"_item");
            map.put("masterClass",className);
            map.put("model","ApesModel");
        }
        if (tableName.indexOf("Exec")> -1){
            map.put("model","ApesModel");
            if (type.equals(MASTER)){
                 //添加唯一索引
                map.put("indexs",indexs);
            }
        }
        map.put("field",field);
        map.put("method",method);

        return createImport(type,map) + createBody(type,map,isMultiple);
    }

    public  static String createReposity(String packageName,String className){

        StringBuffer entity = new StringBuffer();
        entity.append("package ${packageName};\n");
        entity.append("import com.apes.framework.jpa.repository.CustomRepository;\n");
        entity.append("import ${packageName}.${className};\n");
        entity.append("public interface ${className}Repository extends CustomRepository<${className}, String> {\n" +
                      "}");
        Map<String, String> map = new HashMap<String, String>();
        map.put("packageName",packageName+".repository");
        map.put("className",className);
        String resultStr = StringUtil.resolvePlaceholders(entity.toString(), map);
        return resultStr;
    }


}
